

package features;

import com.vividsolutions.jts.geom.Coordinate;

import java.awt.BorderLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
import java.nio.channels.FileChannel;
import java.nio.charset.Charset;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import org.geotools.data.CachingFeatureSource;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.ShapefileDataStore;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.referencing.CRS;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.data.JFileDataStoreChooser;
import com.vividsolutions.jts.geom.Geometry;
import java.awt.Container;
import java.io.BufferedInputStream;
import java.io.InputStream;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import org.geotools.data.shapefile.dbf.DbaseFileReader.Row;
import org.geotools.data.shapefile.shp.ShapefileException;
import org.geotools.data.shapefile.shp.ShapefileHeader;
import org.geotools.feature.FeatureCollection;
import org.geotools.feature.FeatureIterator;
import org.opengis.feature.Feature;
import org.opengis.feature.type.AttributeType;
import org.opengis.feature.type.FeatureType;

/**
 * GeoTools Quickstart demo application. Prompts the user for a shapefile
 * and displays its contents on the screen in a map frame
 *
 * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/demo/example/src/main/java/org/geotools/demo/Quickstart.java $
 */
public class Escala {

    static private String  URLDBF;

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile
     * and displays its contents on the screen in a map frame
     */
    public static void main(String[] args) throws Exception  {
        ///ABRIR O SHAPEFILE
        String [] vetor = new String[80];
        int i=0;
        File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();

        // Create a map context and add our shapefile to it
        MapContext map = new DefaultMapContext();
        map.setTitle("Quickstart");
        map.addLayer(featureSource, null);
        ////UTILIZA O FILE do SHP PARA ABRIR O DBF
        String dbf = file.toString();
        dbf= dbf.substring(0, dbf.length() - 3);
        dbf = dbf + "dbf";
        File f = new File (dbf);



///////////////////////ABRIR O DBF


            FileInputStream fis = new FileInputStream( f );
            DbaseFileReader dbfReader =  new DbaseFileReader(fis.getChannel(),
            false,  Charset.forName("ISO-8859-1"));


            while ( dbfReader.hasNext() ){//percorre a linha
                
                Object[] fields = dbfReader.readEntry();
                System.out.print(dbfReader.getHeader().getNumFields());
           // System.out.print(dbfReader.getHeader().getFieldName());
               for (int j = 0; j < fields.length; j++) {//aqui a coluna

              
              
              System.out.print(fields[j] + "||");

                }
                i++;
                System.out.println(" ");
            }

            dbfReader.close();
            fis.close();

      JMapFrame.showMap(map);


      


    }


}

//class Tabela extends JFrame
//{
//    public Tabela(String [] vetor)
//    {
//        setTitle("TABELA DE ATRIBUTOS");
//        setSize(200, 300);
//        addWindowListener(new WindowAdapter() {
//        public void windowClosing(WindowEvent e)
//            {}
//        }
//            );
//        JTable tabela = new JTable(5,3);
//        //tabela.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// ativa barra de rolagem horizontal
//        //tabela.setModel(new ModeloTabela(null, 5, 2));
//        tabela.setVisible(true);
//
//    }
//}